import VueRouter from "vue-router";
import Vue from "vue";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "Home",
    component: () => import("@/views/Home.vue"),
    meta: {
      title: "ReganC",
    },
  },
  {
    path: "/category/:name",
    name: "Category",
    component: () => import("@/views/Category.vue"),
    meta: {
      title: "文章分类",
    },
  },
  {
    path: "/tag/:name",
    name: "Tag",
    component: () => import("@/views/Tag.vue"),
    meta: {
      title: "标签",
    },
  },
  {
    path: "/timeline",
    name: "TimeLine",
    component: () => import("@/views/TimeLine.vue"),
    meta: {
      title: "时间线",
    },
  },
  {
    path: "/message",
    name: "Message",
    component: () => import("@/views/Message.vue"),
    meta: {
      title: "展示板",
    },
  },
  {
    path: "/post/:id",
    name: "Article",
    component: () => import("@/views/Article.vue"),
    meta: {
      title: "文章详情",
    },
  },
  {
    path: "/about",
    name: "About",
    component: () => import("@/views/About.vue"),
    meta: {
      title: "关于",
    },
  },
  {
    path: "/links",
    name: "Links",
    component: () => import("@/views/Links.vue"),
    meta: {
      title: "友联",
    },
  },
  {
    path: "/talk",
    name: "Talk",
    component: () => import("@/views/Talk.vue"),
    meta: {
      title: "交流",
    },
  },
  {
    path: "/photo",
    name: "Photo",
    component: () => import("@/views/Photo.vue"),
    meta: {
      title: "照片",
    },
  },
  {
    path: "/three",
    name: "Three",
    component: () => import("@/views/Three.vue"),
    meta: {
      title: "3D",
    },
  },
  {
    path: "/threeone",
    name: "ThreeOne",
    component: () => import("@/components/three/ThreeOne.vue"),
    meta: {
      title: "3D1",
    },
  },
  {
    path: "/threetwo",
    name: "ThreeTwo",
    component: () => import("@/components/three/ThreeTwo.vue"),
    meta: {
      title: "3D2",
    },
  },
  {
    path: "/threethree",
    name: "ThreeThree",
    component: () => import("@/components/three/ThreeThree.vue"),
    meta: {
      title: "3D3",
    },
  },
  {
    path: "/threefor",
    name: "ThreeFor",
    component: () => import("@/components/three/ThreeFor.vue"),
    meta: {
      title: "3D4",
    },
  },
  {
    path: "/messageown",
    name: "MessageOwn",
    component: () => import("@/views/Messageown.vue"),
    meta: {
      title: "自制留言板",
    },
  },
];

const router = new VueRouter({
  // 路由使用his模式--路由直接拼接在URL后
  mode: "history",
  routes,
});

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

// 在路由跳跃之前执行，可用于权限管理，这里改一下文档标题
router.beforeEach((to, from, next) => {
  console.log(from);
  if (to.meta.title) {
    document.title = to.meta.title;
  }
  next();
});

export default router;
